package org.hot100.h005;

/**
 * @Author: wjy
 * @Date: 2022/1/5 16:03
 */
public class Solution {

    /**
     * 给你一个字符串 s，找到 s 中最长的回文子串。
     *
     * 示例 1：
     *
     * 输入：s = "babad"
     * 输出："bab"
     * 解释："aba" 同样是符合题意的答案。
     *
     * 示例 2：
     *
     * 输入：s = "cbbd"
     * 输出："bb"
     *
     * 示例 3：
     *
     * 输入：s = "a"
     * 输出："a"
     *
     * 示例 4：
     *
     * 输入：s = "ac"
     * 输出："a"
     *
     * @param args
     */
    public static void main(String[] args) {

        //String str = "bab";

        String str = "babad";

        //String str = "aacabdkacaa";

        //String str = "abcdcba";

        Solution solution = new Solution();

        System.out.println(solution.longestPalindrome5(str));

    }

    public String longestPalindrome(String s) {
        // 特殊用例判断
        int len = s.length();
        if (len < 2) {
            return s;
        }

        int maxLen = 1;
        int begin = 0;

        // dp[i][j] 表示 s[i, j] 是否是回文串
        boolean[][] dp = new boolean[len][len];
        char[] charArray = s.toCharArray();

/*        for (int i = 0; i < len; i++) {
            dp[i][i] = true;
        }*/
        for (int j = 1; j < len; j++) {
            for (int i = 0; i < j; i++) {
                if (charArray[i] != charArray[j]) {
                    dp[i][j] = false;
                } else {
                    if (j - i < 3) {
                        dp[i][j] = true;
                    } else {
                        dp[i][j] = dp[i + 1][j - 1];
                    }
                }

                // 只要 dp[i][j] == true 成立，就表示子串 s[i..j] 是回文，此时记录回文长度和起始位置
                if (dp[i][j] && j - i + 1 > maxLen) {
                    maxLen = j - i + 1;
                    begin = i;
                }
            }
        }
        return s.substring(begin, begin + maxLen);
    }

    public String longestPalindrome2(String s) {

        // 长度为1直接返回该字符串
        if (s.length() == 1) {
            return s;
        }

        int length = s.length();

        char[] chars = s.toCharArray();

        // 矩阵方式标记左右是否为相同字母
        boolean[][] flag = new boolean[length][length];

        // begin 记录最长回文子串的起始下标
        // childLength 记录最长回文子串的长度
        int begin = 0, childLength = 1;

        for (int i = 1; i < length; i++) {
            for (int j = 0; j < i; j++) {
                if (chars[j] == chars[i]) {
                    if (i - j < 3) {

                        // 长度小于 3 的直接设置为 true 如：1 2 1
                        flag[i][j] = true;
                    } else {

                        // 收缩回文范围检查 '上一次' 是否为回文
                        flag[i][j] = flag[i - 1][j + 1];
                    }
                }

                if (flag[i][j] && i - j + 1 > childLength) {
                    begin = j;
                    childLength = i - j + 1;
                }
            }
        }

        return s.substring(begin, childLength + begin);
    }

    public String longestPalindrome3(String s) {
        int length = s.length();
        int high = s.length();
        int begin = 0, size = 1;

        boolean[][] booleans = new boolean[length][high];

        for (int i = 1; i < s.length(); i++) {
            for (int j = 0; j < i; j++) {
                if (s.charAt(j) == s.charAt(i)) {
                    if ((i - j) < 3) {
                        booleans[i][j] = true;
                    } else {
                        booleans[i][j] = booleans[i - 1][j + 1];
                    }
                }
                if (booleans[i][j] && i - j + 1 > size) {
                    begin = j;
                    size = i - j + 1;
                }
            }
        }

        return s.substring(begin, size + begin);
    }

    public String longestPalindrome4(String s) {

        char[] chars = s.toCharArray();
        int length = chars.length, size = 0, begin = 0;
        boolean[][] booleans = new boolean[length][length];

        for (int i = 1; i < length; i++) {
            for (int j = 0; j < i; j++) {
                if (chars[j] == chars[i]) {
                    if (i - j < 3) {
                        booleans[i][j] = true;
                    } else {
                        booleans[i][j] = booleans[i - 1][j + 1];
                    }
                }
                if (booleans[i][j] && i - j + 1 > size) {
                    begin = j;
                    size = i - j + 1;
                }
            }
        }
        return s.substring(begin, size + begin);
    }

    public String longestPalindrome5(String s) {
        boolean[][] booleans = new boolean[s.length()][s.length()];
        int begin = 0, length = 0;
        for (int i = 1; i < s.length(); i++) {
            for (int j = 0; j < i; j++) {
                if (s.charAt(i) == s.charAt(j)) {
                    if (i - j < 3) {
                        booleans[i][j] = true;
                    } else {
                        booleans[i][j] = booleans[i - 1][j + 1];
                    }

                }
                if (booleans[i][j] && length < i - j + 1) {
                    begin = j;
                    length = i - j + 1;
                }
            }
        }

        return s.substring(begin, begin + length);
    }

}
